```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式系统CAP定理深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #2563eb 0%, #1e40af 100%);
        }
        .section-title {
            position: relative;
            padding-left: 1.5rem;
        }
        .section-title:before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.4em;
            height: 1.2em;
            width: 0.4rem;
            background-color: #2563eb;
            border-radius: 0.2rem;
        }
        .concept-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .concept-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .visualization-container {
            background-color: white;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        .tooltip {
            position: relative;
            display: inline-block;
        }
        .tooltip .tooltip-text {
            visibility: hidden;
            width: 200px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            margin-left: -100px;
            opacity: 0;
            transition: opacity 0.3s;
        }
        .tooltip:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <div class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4 leading-tight">分布式系统的<span class="text-yellow-300">CAP定理</span></h1>
                    <p class="text-xl mb-8 opacity-90">深入理解一致性、可用性与分区容错的权衡之道</p>
                    <div class="flex items-center">
                        <div class="bg-white text-blue-800 px-4 py-2 rounded-full font-semibold mr-4">
                            <i class="fas fa-calendar-alt mr-2"></i>1998年提出
                        </div>
                        <div class="bg-white text-blue-800 px-4 py-2 rounded-full font-semibold">
                            <i class="fas fa-user-tie mr-2"></i>Eric Brewer
                        </div>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="visualization-container p-6 w-full max-w-md">
                        <div class="mermaid">
                            graph TD
                                CAP["CAP定理"] --> C("一致性")
                                CAP --> A("可用性")
                                CAP --> P("分区容错")
                                C -->|"强一致性要求"| C1["数据同步完成才能响应"]
                                A -->|"高可用要求"| A1["立即响应,数据可能不一致"]
                                P -->|"网络分区必然存在"| P1["必须容忍分区故障"]
                                C & A -->|"只能二选一"| CA["CA系统(理想但不现实)"]
                                C & P -->|"CP系统"| CP["如:Zookeeper"]
                                A & P -->|"AP系统"| AP["如:Eureka"]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 section-title">CAP定理概述</h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6 mb-8">
                <p class="text-lg mb-4">1998年，加州大学的计算机科学家 Eric Brewer 提出，分布式系统有三个核心指标：</p>
                <div class="grid md:grid-cols-3 gap-6 mb-6">
                    <div class="concept-card bg-blue-50 p-6 rounded-lg border border-blue-100">
                        <div class="flex items-start mb-4">
                            <div class="bg-blue-100 text-blue-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-check-circle text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold text-blue-800">一致性 (Consistency)</h3>
                        </div>
                        <p class="text-gray-700">写操作之后的读操作必须返回最新值，所有节点数据一致</p>
                    </div>
                    <div class="concept-card bg-green-50 p-6 rounded-lg border border-green-100">
                        <div class="flex items-start mb-4">
                            <div class="bg-green-100 text-green-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-plug text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold text-green-800">可用性 (Availability)</h3>
                        </div>
                        <p class="text-gray-700">只要收到用户请求，服务器就必须给出回应，不保证数据最新</p>
                    </div>
                    <div class="concept-card bg-purple-50 p-6 rounded-lg border border-purple-100">
                        <div class="flex items-start mb-4">
                            <div class="bg-purple-100 text-purple-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-network-wired text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold text-purple-800">分区容错 (Partition tolerance)</h3>
                        </div>
                        <p class="text-gray-700">系统能够容忍网络分区故障，节点间通信可能失败</p>
                    </div>
                </div>
                <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 rounded">
                    <div class="flex">
                        <div class="flex-shrink-0 text-yellow-500">
                            <i class="fas fa-exclamation-circle text-xl"></i>
                        </div>
                        <div class="ml-3">
                            <p class="text-yellow-700 font-semibold">CAP原则</p>
                            <p class="text-yellow-700">这三个指标不可能同时做到，最多只能同时实现两点，不可能三者兼顾。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Details Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 section-title">CAP三要素详解</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-10">
                <!-- Partition Tolerance -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <h3 class="text-2xl font-bold mb-4 text-purple-800 flex items-center">
                            <i class="fas fa-network-wired mr-3"></i>分区容错性 (Partition tolerance)
                        </h3>
                        <p class="mb-4">大多数分布式系统都分布在多个子网络（分区）。分区容错意味着系统能够容忍节点间的通信失败。</p>
                        <div class="bg-gray-100 p-4 rounded-lg mb-4">
                            <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651295476274-a2cf1b6a-0af4-4cd6-9ca6-63565ce5739e.png" alt="分区容错示意图" class="rounded-lg w-full">
                            <p class="text-sm text-gray-500 mt-2 text-center">G1 和 G2 是两台跨区服务器，G1向G2发送消息可能失败</p>
                        </div>
                        <div class="bg-purple-50 p-4 rounded-lg">
                            <p class="font-semibold text-purple-800"><i class="fas fa-lightbulb mr-2"></i>关键结论：</p>
                            <p>分区容错无法避免（P总是成立），因此系统设计时只能在C和A之间做选择。</p>
                        </div>
                    </div>
                </div>
                
                <!-- Consistency vs Availability -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <h3 class="text-2xl font-bold mb-4 text-red-600">一致性与可用性的矛盾</h3>
                        <div class="mb-4">
                            <p class="mb-2"><strong>一致性要求：</strong>写操作后必须保证所有节点数据一致</p>
                            <div class="bg-gray-100 p-3 rounded-lg mb-3">
                                <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651295477176-23420223-6248-403e-9f60-cf2379477081.png" alt="一致性示意图" class="rounded-lg w-full">
                                <p class="text-sm text-gray-500 mt-2 text-center">G1写入v1后需要同步到G2才能保证一致性</p>
                            </div>
                        </div>
                        <div class="mb-4">
                            <p class="mb-2"><strong>可用性要求：</strong>任何请求必须立即响应，不保证数据最新</p>
                            <div class="bg-gray-100 p-3 rounded-lg mb-3">
                                <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651295476984-9da6f959-c212-40f4-b5ae-174e22ccd8e2.png" alt="可用性示意图" class="rounded-lg w-full">
                                <p class="text-sm text-gray-500 mt-2 text-center">G2立即响应v0而不等待同步</p>
                            </div>
                        </div>
                        <div class="bg-red-50 p-4 rounded-lg">
                            <p class="font-semibold text-red-700"><i class="fas fa-exclamation-triangle mr-2"></i>矛盾核心：</p>
                            <p>保证一致性需要锁定节点同步数据（牺牲可用性）；保证可用性则无法确保数据一致。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Practical Applications -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 section-title">分布式中间件的CAP选择</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <!-- Zookeeper -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden border-l-4 border-blue-500">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 text-blue-800 rounded-lg p-3 mr-4">
                                <i class="fas fa-archive text-2xl"></i>
                            </div>
                            <div>
                                <h3 class="text-2xl font-bold text-blue-800">Zookeeper</h3>
                                <div class="flex mt-1">
                                    <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded mr-2">CP系统</span>
                                </div>
                            </div>
                        </div>
                        <p class="mb-4">ZK保证强一致性，在做节点间数据同步时，整个集群可能不可用。</p>
                        <ul class="space-y-2 mb-4">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>Leader选举期间(30-120s)整个集群不可用</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>超过半数节点宕机后集群无法使用</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>适合需要强一致性的场景</span>
                            </li>
                        </ul>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <p class="font-semibold text-blue-800"><i class="fas fa-info-circle mr-2"></i>实现原理：</p>
                            <p>服务启动创建临时节点，节点删除后通知监听服务更新列表，保证数据强一致性。</p>
                        </div>
                    </div>
                </div>
                
                <!-- Eureka -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden border-l-4 border-green-500">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-green-100 text-green-800 rounded-lg p-3 mr-4">
                                <i class="fas fa-cloud text-2xl"></i>
                            </div>
                            <div>
                                <h3 class="text-2xl font-bold text-green-800">Eureka</h3>
                                <div class="flex mt-1">
                                    <span class="bg-green-100 text-green-800 text-xs px-2 py-1 rounded mr-2">AP系统</span>
                                </div>
                            </div>
                        </div>
                        <p class="mb-4">Eureka优先保证可用性，节点平等，部分节点故障不影响整体服务。</p>
                        <ul class="space-y-2 mb-4">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>节点故障不会导致注册服务瘫痪</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>15分钟内85%节点无心跳则进入保护模式</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>数据最终一致性而非强一致性</span>
                            </li>
                        </ul>
                        <div class="bg-green-50 p-4 rounded-lg">
                            <p class="font-semibold text-green-800"><i class="fas fa-info-circle mr-2"></i>自我保护机制：</p>
                            <p>大量心跳失败时认为网络异常，不再剔除服务，待网络恢复后自动同步。</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Nacos -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 rounded-lg p-3 mr-4">
                            <i class="fas fa-server text-2xl"></i>
                        </div>
                        <div>
                            <h3 class="text-2xl font-bold text-purple-800">Nacos</h3>
                            <div class="flex mt-1">
                                <span class="bg-purple-100 text-purple-800 text-xs px-2 py-1 rounded mr-2">AP或CP架构</span>
                            </div>
                        </div>
                    </div>
                    <p class="mb-4">Nacos可根据实例类型(临时/持久)选择AP或CP模式。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div class="border border-blue-200 rounded-lg p-4">
                            <h4 class="font-bold text-blue-700 mb-2 flex items-center">
                                <i class="fas fa-bolt mr-2"></i>临时实例 (CP架构)
                            </h4>
                            <ul class="space-y-2 text-sm">
                                <li class="flex items-start">
                                    <i class="fas fa-circle text-xs text-blue-500 mt-1 mr-2"></i>
                                    <span>失去心跳后立即删除实例</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-circle text-xs text-blue-500 mt-1 mr-2"></i>
                                    <span>保证数据强一致性</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-circle text-xs text-blue-500 mt-1 mr-2"></i>
                                    <span>适合流量突增的弹性扩容场景</span>
                                </li>
                            </ul>
                        </div>
                        <div class="border border-green-200 rounded-lg p-4">
                            <h4 class="font-bold text-green-700 mb-2 flex items-center">
                                <i class="fas fa-shield-alt mr-2"></i>持久实例 (AP架构)
                            </h4>
                            <ul class="space-y-2 text-sm">
                                <li class="flex items-start">
                                    <i class="fas fa-circle text-xs text-green-500 mt-1 mr-2"></i>
                                    <span>失去心跳后只标记不删除</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-circle text-xs text-green-500 mt-1 mr-2"></i>
                                    <span>保证服务高可用性</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-circle text-xs text-green-500 mt-1 mr-2"></i>
                                    <span>适合需要长期监控的场景</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                    
                    <div class="bg-gray-100 p-4 rounded-lg">
                        <div class="flex">
                            <div class="flex-shrink-0">
                                <i class="fas fa-code text-gray-500"></i>
                            </div>
                            <div class="ml-3 overflow-x-auto">
                                <p class="font-mono text-sm text-gray-800">
                                    spring.cloud.nacos.discovery.ephemeral=false # 持久化实例(AP)<br>
                                    spring.cloud.nacos.discovery.ephemeral=true # 临时实例(CP)
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Advanced Topics -->
        <section>
            <h2 class="text-3xl font-bold mb-6 section-title">高级主题</h2>
            
            <!-- Brain Split Prevention -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <i class="fas fa-brain mr-3 text-indigo-600"></i>防止脑裂问题
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div>
                            <h4 class="font-bold mb-2 text-lg">选举机制</h4>
                            <p class="mb-4">要求节点获取的投票数量 > 总节点数/2，确保网络分区时最多只有一个小集群能选出leader。</p>
                            <div class="bg-gray-100 p-4 rounded-lg">
                                <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1659081184186-4500b1f5-db81-465a-b722-d1bbe2c41e62.png" alt="脑裂防止示意图" class="rounded-lg w-full">
                                <p class="text-sm text-gray-500 mt-2 text-center">网络分区时选举机制防止脑裂</p>
                            </div>
                        </div>
                        <div>
                            <h4 class="font-bold mb-2 text-lg">奇数节点原则</h4>
                            <p class="mb-2">集群节点推荐使用奇数个，原因：</p>
                            <ul class="space-y-2 mb-4">
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                    <span>容错能力相同下比偶数节点更节省资源</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                    <span>避免偶数节点对半分区无法选举leader</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                    <span>5节点可容忍2节点故障，6节点同样只能容忍2节点故障</span>
                                </li>
                            </ul>
                            <div class="bg-indigo-50 p-4 rounded-lg">
                                <p class="font-semibold text-indigo-800"><i class="fas fa-lightbulb mr-2"></i>实际应用：</p>
                                <p>Zookeeper、Etcd等CP系统通常采用3或5节点的集群配置。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- BASE Theory -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <i class="fas fa-atom mr-3 text-teal-600"></i>BASE理论
                    </h3>
                    <p class="mb-6">为了弥补CAP中强一致性带来的可用性降低问题，提出BASE理论作为补充：</p>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="bg-teal-50 p-5 rounded-lg border border-teal-100">
                            <h4 class="font-bold text-lg mb-3 text-teal-800 flex items-center">
                                <i class="fas fa-check-square mr-2"></i>基本可用 (BA)
                            </h4>
                            <p class="text-gray-700">系统出现故障时，允许损失部分可用性，如响应时间延长或部分功能降级。</p>
                        </div>
                        <div class="bg-teal-50 p-5 rounded-lg border border-teal-100">
                            <h4 class="font-bold text-lg mb-3 text-teal-800 flex items-center">
                                <i class="fas fa-cloud mr-2"></i>软状态 (S)
                            </h4>
                            <p class="text-gray-700">允许系统存在中间状态，不同节点的数据副本同步可能存在延迟。</p>
                        </div>
                        <div class="bg-teal-50 p-5 rounded-lg border border-teal-100">
                            <h4 class="font-bold text-lg mb-3 text-teal-800 flex items-center">
                                <i class="fas fa-hourglass-end mr-2"></i>最终一致性 (E)
                            </h4>
                            <p class="text-gray-700">经过一段时间同步后，所有数据副本最终能达到一致状态。</p>
                        </div>
                    </div>
                    
                    <div class="mt-6 bg-gray-100 p-4 rounded-lg">
                        <div class="flex">
                            <div class="flex-shrink-0">
                                <i class="fas fa-quote-left text-gray-500"></i>
                            </div>
                            <div class="ml-3">
                                <p class="italic text-gray-700">"BASE理论的核心思想是即使无法做到强一致性，但每个应用都可以根据自身业务特点，采用适当的方式来使系统达到最终一致性。"</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 添加卡片悬停效果
        document.querySelectorAll('.concept-card').forEach(card => {
            card.addEventListener('mouseenter', function() {
                const colorClass = this.classList.contains('bg-blue-50') ? 'blue' : 
                                 this.classList.contains('bg-green-50') ? 'green' : 'purple';
                const shadowColor = {
                    'blue': '0 10px 15px -3px rgba(59, 130, 246, 0.3)',
                    'green': '0 10px 15px -3px rgba(16, 185, 129, 0.3)',
                    'purple': '0 10px 15px -3px rgba(139, 92, 246, 0.3)'
                };
                this.style.boxShadow = shadowColor[colorClass];
            });
            card.addEventListener('mouseleave', function() {
                this.style.boxShadow = '';
            });
        });
        
        // 添加平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```